package controller;

import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Flight;
import model.FlightIO;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * Servlet implementation class PrintTickets
 */
public class PrintTickets extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PrintTickets() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * The PrintTieckets servlet takes the passenger info from the form and
	 * creates a pdf. That pdf is then displayed for the user to print.
	 * 
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  //Get the output stream
		  ServletOutputStream os = response.getOutputStream();
		  
		  //Set the response content type to PDF
		  response.setContentType("application/pdf");
		  
		  //Create Document
		  Document doc = new Document();
		  
		  String[] names = request.getParameterValues("name");
		  String[] ages = request.getParameterValues("age");
		  String[] gender = request.getParameterValues("gender");
		  String[] flightIDs = request.getParameterValues("flightID");
		  String[] classType = request.getParameterValues("classType");

		 
		  FlightIO flightIO = new FlightIO();
		  
		  
		  //create some special styles and font sizes
		  Font bfBold18 = new Font(FontFamily.TIMES_ROMAN, 18, Font.BOLD, new BaseColor(0, 0, 0));
		  Font bf12 = new Font(FontFamily.TIMES_ROMAN, 12);
		 
		  try{
		    
		   //create an instance of the PdfWriter using the output stream
		   PdfWriter.getInstance(doc, os);
		 
		   //document header properties
		   doc.addAuthor("FlightApp");
		   doc.addCreationDate();
		   doc.addProducer();
		   doc.addCreator("FLightApp.com");
		   doc.addTitle("Tickets");
		   doc.setPageSize(PageSize.LETTER);
		   doc.open();
		 
		   //add a new paragraph
		   doc.add( new Paragraph("Tickets", bfBold18)); 
		   doc.add(new Paragraph(" "));
		   SimpleDateFormat fmt = new SimpleDateFormat("h:mm a");
		   SimpleDateFormat fmt2 = new SimpleDateFormat("MM/dd/yyyy");
		   for(int j=0; j<flightIDs.length; j++){
			   Flight flight = flightIO.getFlight(Integer.parseInt(flightIDs[j]));
			   doc.add( new Paragraph("FLIGHT: " + flight.getId(), bfBold18));
			   doc.add( new Paragraph(names[j] + ", " + gender[j] + ", " + ages[j], bf12));
			   doc.add( new Paragraph("Flight Number: " + flight.getId()));
			   doc.add( new Paragraph(flight.getSource() + " to " + flight.getDestination() + " on " + fmt2.format(flight.getDeparture()) , bf12));
			   doc.add( new Paragraph("Class: " + classType[j], bf12));
			   doc.add( new Paragraph("Departure: " + fmt.format(flight.getDeparture()) + " Arrival: " + fmt.format(flight.getArrival()), bf12));
			   doc.add( new Paragraph(" "));
			   doc.add( new Paragraph(" "));
		   }
		   
		                                                  	 
		   doc.close();
		 
		  }catch(DocumentException e){
		   e.printStackTrace();
		  }
		  catch(Exception e){
		   e.printStackTrace();
		  }
	}

}
